using System;
using System.Web.UI.WebControls;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Web.Editors;

using FeatureCenter.Web.Controls;
using DevExpress.ExpressApp.Web.TestScripts;
using FeatureCenter.Module.PropertyEditors;
using DevExpress.ExpressApp.Model;

namespace FeatureCenter.Module.Web.PropertyEditors {
    [PropertyEditor(typeof(Int32), FeatureCenterEditorAliases.CustomIntegerEditor, false)]
    public class CustomIntegerEditor : WebPropertyEditor, ITestable {
        StarRatingControl rater = null;
        public CustomIntegerEditor(Type objectType, IModelMemberViewItem info) : base(objectType, info) { }
		private StarRatingControl CreateRater() {
			StarRatingControl rater = new StarRatingControl();
            rater.ImageOnUrl = DevExpress.ExpressApp.Utils.ImageLoader.Instance.GetImageInfo("StarOn").ImageUrl;
            rater.ImageOffUrl = DevExpress.ExpressApp.Utils.ImageLoader.Instance.GetImageInfo("StarOff").ImageUrl;
            rater.ImageOverUrl = DevExpress.ExpressApp.Utils.ImageLoader.Instance.GetImageInfo("StarOver").ImageUrl;
            rater.ID = "Rater";
            return rater;            
        }        
        protected override WebControl CreateViewModeControlCore() {
			StarRatingControl rater = CreateRater();
            rater.Enabled = false;
            return rater;
        }
        protected override WebControl CreateEditModeControlCore() {
			rater = CreateRater();
            rater.ValueChanged += new EventHandler(EditValueChangedHandler);
            return rater;
        }
        protected override object GetControlValueCore() {
			return ((StarRatingControl)Editor).Value;
        }
        protected override void ReadEditModeValueCore() {
			((StarRatingControl)Editor).Value = (int)PropertyValue;
        }
        public override void BreakLinksToControl(bool unwireEventsOnly) {
            if(rater != null) {
                rater.ValueChanged -= new EventHandler(EditValueChangedHandler);
            }
            base.BreakLinksToControl(unwireEventsOnly);
        }
        protected override void ReadViewModeValueCore() {
			((StarRatingControl)InplaceViewModeEditor).Value = (int)PropertyValue;
        }
        protected override IJScriptTestControl GetInplaceViewModeEditorTestControlImpl() {
            return new JSASPxCustomTestControl();
        }
        protected override IJScriptTestControl GetEditorTestControlImpl() {
            return new JSASPxCustomTestControl();
        }
	}
    public class JSASPxCustomTestControl : IJScriptTestControl {
        public const string ClassName = "StarRatingTestControl";
        private static StandardTestControlScriptsDeclaration scriptDeclatation;
        static JSASPxCustomTestControl() {
            scriptDeclatation = new ASPxStandardTestControlScriptsDeclaration();
            ((ASPxStandardTestControlScriptsDeclaration)scriptDeclatation).InitControlFunctionBody = @"
                var control = eval('window.' + this.id);
                if(control) {
                    this.control = control;
                }
                else {
                    this.control = eval(this.id + '_MainTable');
                }
            ";
            scriptDeclatation.IsEnabledFunctionBody = @"
                return this.control.Enabled;
            ";
            scriptDeclatation.GetTextFunctionBody = @"
			    var value = this.control.Value;			
			    return (value == null) ? '' : value.toString();
			";
            scriptDeclatation.SetTextFunctionBody = @"
                if(this.control.SetValue) {
                    this.control.SetValue(value)
                }
            ";
        }
        public string JScriptClassName {
            get { return ClassName; }
        }
        public TestScriptsDeclarationBase ScriptsDeclaration {
            get {
                return scriptDeclatation;
            }
        }
    }
}
